package com.sztx.worker_monitor.web.handle;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sztx.worker_monitor.dataaccess.dao.mysql.TBSchedNameDAO;
import com.sztx.worker_monitor.dataaccess.domain.TBSchedNameDO;
import com.sztx.worker_monitor.web.model.ResultStatus;
import com.sztx.worker_monitor.web.model.TBSchedNameModel;

@Component
public class TBSchedNameHandle {
	@Autowired(required = false)
	private TBSchedNameDAO tbschedNameDAO;

	public List<String> getSchedNameList(){
		
		List<TBSchedNameDO> schedNameDOs = tbschedNameDAO.getAllSchedName();
		List<String> schedNames = new ArrayList<String>();
		for(TBSchedNameDO schedNameDO:schedNameDOs){
			String schedName = schedNameDO.getName();
			schedNames.add(schedName);
		}
		return schedNames;	
	}
	
	public String  delSchedName(String schedName){
		if(schedName == null || "".equals(schedName)) return "输入名称错误，无法删除";
		
		TBSchedNameDO schedNameDO = tbschedNameDAO.findItemBySchedName(schedName);
		if(schedNameDO == null){
			return "该名称不存在";
		} 
		else{
			tbschedNameDAO.deleteSchedName(schedName);
		}
		return null;
	}
	
	public String  addSchedName(String schedName){
		
		if(schedName == null || "".equals(schedName)) return "输入名称错误，无法添加";
		
		TBSchedNameDO schedNameDO = tbschedNameDAO.findItemBySchedName(schedName);
		if(schedNameDO == null){
			tbschedNameDAO.insertSchedName(schedName);
		} 
		else if(schedNameDO.getStatus() == 1){
			tbschedNameDAO.reAddSchedName(schedName);
		}
		else{
			return "该名称已经存在";
		}
		return null;	
		
	} 
	
	public  ResultStatus getAllSchedName(){
		
		List<TBSchedNameDO>  tbschedNameDOs =tbschedNameDAO.getAllSchedName();
		
		List<TBSchedNameModel> names = new ArrayList<TBSchedNameModel>();
		
		if(tbschedNameDOs == null){ 
			return new ResultStatus(null, true, names);
		}
		
		for(TBSchedNameDO tbschedNameDO : tbschedNameDOs){
			names.add(new TBSchedNameModel(tbschedNameDO.getId(), tbschedNameDO.getName()));
		}
		return new ResultStatus(null, true, names);
		
	}
	


}
